// pages/invoice/fillIn/index.js
var call = require("../../../utils/request.js");
var email = require("../../../utils/util.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    type: 1,
    is_travel: 0,
    orders: [], // 需要开发票的订单id
    amount: 0.00, // 开票金额
    invoiceTitle: {}, // 发票抬头
    typeStatus: true, 
  },
  // 获取发票抬头
  chooseInvoiceTitle() {
    wx.chooseInvoiceTitle({
      success: (res) => {
        console.log(res)
        this.setData({ invoiceTitle: res })
        res.type === '0' ? this.setData({ typeStatus: true }) : this.setData({ typeStatus: false })
      },
      fail: (err) => {
        console.error(err)
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
     // 所选项的id options.orders
     // 总金额 options.amount
     this.setData({
      amount: options.amount,
      orders: JSON.parse(options.orders)
    })
    console.log(options.orders,options.amount)
  },
  // 是否发送行程单
  checkChange: function (e) {
    e.detail.value.length > 0 ? this.setData({ is_travel: 1 }) : this.setData({ is_travel: 0 })
    console.log('radio发生change事件，携带value值为：', e.detail.value)
  },
  // 提交
  formSubmit: function (e) {
    let that = this;
    let value = e.detail.value;
    let invoiceTitle = this.data.invoiceTitle;
    if(JSON.stringify(invoiceTitle) != "{}"){
      if(email.isEmail(value.email)){
        let rise = '';
        invoiceTitle.type == 0 ? rise = 1 : rise = 2;
        let isall = '';
        this.data.orders.length > 0 ? isall = 0 : isall = 1;
        console.log(invoiceTitle)
        let Data = {
          rows:{
            rise,
            isall,
            company: invoiceTitle.title,
            taxno: invoiceTitle.taxNumber,
            telephone: invoiceTitle.telephone,
            address: invoiceTitle.companyAddress,
            account_name: invoiceTitle.bankName,
            account: invoiceTitle.bankAccount,
            email: value.email,
            orders: that.data.orders,
            remark: value.remark,
            is_travel: that.data.is_travel
          }
        }
        call.Form('/api/Invoice/apply',Data,
        (res) => {
          if(res.code == 1){
            wx.showToast({ icon: 'none',title: '提交成功' })
            var pages = getCurrentPages();//当前页面栈
            var beforePage = pages[pages.length - 2];//获取上一个页面实例对象
            setTimeout(function () {
              wx.navigateBack({
                delta: 1, //返回的页面数，如果 delta 大于现有页面数，则返回到首页。
                success: function () {
                  if (beforePage.route == 'pages/invoice/list/index') {
                    beforePage.onLoadList() //这个函数式调用接口的函数
                  }
                }
              })
            }, 500)
          }else{
            wx.showToast({ icon: 'none',title: res.msg })
          }
        },(err) => {
          console.log(err)
        })
      }else{
        wx.showToast({ icon: 'none',title: '电子邮件有误' })
      }
    }else{
      wx.showToast({ icon: 'none',title: '请获取发票抬头' })
    }
  },
})